import os
import json
import time
from pathlib import Path

class SimpleCache:
    def __init__(self, cache_dir='cache'):
        """简单的文件系统缓存实现"""
        self.cache_dir = Path(cache_dir)
        self.cache_dir.mkdir(exist_ok=True)
        self._memory_cache = {}

    def get(self, key):
        """获取缓存值"""
        # 先检查内存缓存
        if key in self._memory_cache:
            return self._memory_cache[key]
        
        # 检查文件缓存
        try:
            cache_file = self.cache_dir / f"{key}.json"
            if cache_file.exists():
                with cache_file.open('r', encoding='utf-8') as f:
                    data = json.load(f)
                    self._memory_cache[key] = data
                    return data
        except Exception:
            pass
        return None

    def set(self, key, value):
        """设置缓存值"""
        self._memory_cache[key] = value
        try:
            cache_file = self.cache_dir / f"{key}.json"
            with cache_file.open('w', encoding='utf-8') as f:
                json.dump(value, f, ensure_ascii=False, indent=2)
        except Exception:
            pass
